package com.lvtongka.app.ui.splash

import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.lvtongka.app.base.BaseActivity
import com.lvtongka.app.common.AppUtils
import com.lvtongka.app.databinding.ActivitySplashBinding
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.ui.HomeActivity
import com.lvtongka.app.ui.login.LoginRevisionActivity
import com.lvtongka.app.ui.login.LoginViewModel

class SplashActivity : BaseActivity<ActivitySplashBinding, LoginViewModel>() {
    companion object {
        const val TAG = "SplashActivity_zw_test"
    }

    override fun initBinding(): ActivitySplashBinding {
        return ActivitySplashBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        isBlack = false
        super.onCreate(savedInstanceState)
    }

    override fun initViewModel(): LoginViewModel {
        return ViewModelProvider(this)[LoginViewModel::class.java]
    }


    override fun onResume() {
        super.onResume()
        val isAccept = SPHelper.getBoolean("accept_yd", false)
        if (isAccept) {
            initData2()
        } else {
            binding.flSplash.postDelayed({
                startActivity(Intent(this@SplashActivity, IntroActivity::class.java))
                finish()
            }, 1000)
        }
    }

    override fun initEvent() {

    }

    override fun initData() {

    }

    private fun initData2() {
        binding.flSplash.postDelayed(
            {
                val tp = SPHelper.getLong(SPKConstants.LOGIN_EXPIRE_TIME)
                val isLogin = SPHelper.getBoolean(SPKConstants.LOGIN_IS_LOGIN, false)
                if (System.currentTimeMillis() > tp && tp > 0) {
                    startActivity(Intent(this, LoginRevisionActivity::class.java))
                    finish()
                } else {
                    if (isLogin) {
                        AppUtils.initBugly(this)
                        binding.flSplash.postDelayed(
                            {
                                startActivity(Intent(this, HomeActivity::class.java))
                                finish()
                            }, 1200
                        )
                    } else {
                        startActivity(Intent(this, LoginRevisionActivity::class.java))
                        finish()
                    }
                }
            }, 1200
        )
    }
}